<?php

namespace App\Listeners;

use App\Services\Utils;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;

class QueryLogListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  QueryExecuted  $event
     * @return void
     */
    public function handle(QueryExecuted $event)
    {
        if(!App::environment('production')) {
            $query = $event->sql; // 获取SQL语句
            foreach ($event->bindings as $bind) {
                $query = preg_replace('/\?/', (is_numeric($bind) ? $bind : '\'' . $bind . '\''), $query, 1); // 将SQL中的?替换为实际的值
            }
            Log::info("query: {$query} time: {$event->time}ms");
        }
    }
}
